package xgenerator.generators.jgenerator.impl;


import java.io.File;

import xgenerator.generators.ExtJSGenerator;
import xgenerator.model.ModelMetadata;
import xgenerator.util.StringUtils;

/**
 * <p>
 * Title:Struts23Spring256Hibernate36JspGenerator
 * </p>
 * <p>
 * Description:具体功能见各方法描述
 * </p>
 * <p>
 * Copyright:Copyright (c) 2012
 * </p>
 * 
 * @author <a href="mailto:lishushan@gmail.com">liss</a>
 * @version 1.0
 */
public class Struts13Spring256Hibernate36JspExt3Generator extends Struts23Spring256Hibernate36JspGenerator implements ExtJSGenerator {
	
	/**
	 * /jdk1.5_struts-2.3.x_spring-2.5.x_hibernate-3.x_JSP_ExtJS-3.x
	 */
	public static final String TEMPLATE_PATH = "/jdk1.5_struts-2.3.x_spring-2.5.x_hibernate-3.x_JSP_ExtJS-3.x";
	
	/**
	 * /jdk1.5_struts-2.3.x_spring-2.5.x_hibernate-3.x_JSP_ExtJS-3.x/java
	 */
	public static final String JAVA_TEMPLATE_PATH = TEMPLATE_PATH + "/java";

	/**
	 * /jdk1.5_struts-2.3.x_spring-2.5.x_hibernate-3.x_JSP_ExtJS-3.x/jsp
	 */
	static final String JSP_TEMPLATE_PATH = TEMPLATE_PATH + "/jsp";
	
	/**
	 * /jdk1.5_struts-2.3.x_spring-2.5.x_hibernate-3.x_JSP_ExtJS-3.x/xml
	 */
	public static final String XML_TEMPLATE_PATH = TEMPLATE_PATH + "/xml";
	
	/**
	 * /jdk1.5_struts-2.3.x_spring-2.5.x_hibernate-3.x_JSP_ExtJS-3.x/extjs
	 */
	public static final String EXTJS_TEMPLATE_PATH = TEMPLATE_PATH + "/extjs";

	public String generateRiaCode(ModelMetadata modelMetadata) {
		StringBuffer codes = new StringBuffer();
		
		String createWindowCode = generateCreateWindowCode(modelMetadata);
		if(StringUtils.isNotBlank(createWindowCode)) {
			codes.append("&").append(createWindowCode);
		}
		
		String editWindowCode = generateEditWindowCode(modelMetadata);
		if(StringUtils.isNotBlank(editWindowCode)) {
			codes.append("&").append(editWindowCode);
		}
		
		String viewWindowCode = generateViewWindowCode(modelMetadata);
		if(StringUtils.isNotBlank(viewWindowCode)) {
			codes.append("&").append(viewWindowCode);
		}
		
		String gridPanelCode = generateGridPanelCode(modelMetadata);
		if(StringUtils.isNotBlank(gridPanelCode)) {
			codes.append("&").append(gridPanelCode);
		}
		
		return codes.toString();
	}
	
	public String generateCreateWindowCode(ModelMetadata modelMetadata) {
		String subsystemPackagePath = buildPackagePath(modelMetadata.getSubsystem());
		String modulePackagePath = buildPackagePath(modelMetadata.getModule());
		String createWindowFilePath = modelMetadata.getProjectLocation() + File.separator + modelMetadata.getWebRoot() + File.separator + subsystemPackagePath + File.separator + modulePackagePath + File.separator + modelMetadata.getJavaModelNameLowerCase();
		String createWindowFileName = modelMetadata.getCreateCapPrefix() + modelMetadata.getJavaModelNameLowerCamelCase() + JS_EXT;
		
		String template = EXTJS_TEMPLATE_PATH + "/CreateWindow.ftl";
		return "@" + generateCode(modelMetadata, template, createWindowFilePath, createWindowFileName);
	}

	public String generateEditWindowCode(ModelMetadata modelMetadata) {
		String subsystemPackagePath = buildPackagePath(modelMetadata.getSubsystem());
		String modulePackagePath = buildPackagePath(modelMetadata.getModule());
		String editWindowFilePath = modelMetadata.getProjectLocation() + File.separator + modelMetadata.getWebRoot() + File.separator + subsystemPackagePath + File.separator + modulePackagePath + File.separator + modelMetadata.getJavaModelNameLowerCase();
		String editWindowFileName = modelMetadata.getEditCapPrefix() + modelMetadata.getJavaModelNameLowerCamelCase() + JS_EXT;
		
		String template = EXTJS_TEMPLATE_PATH + "/EditWindow.ftl";
		return "@" + generateCode(modelMetadata, template, editWindowFilePath, editWindowFileName);
	}

	public String generateViewWindowCode(ModelMetadata modelMetadata) {
		String subsystemPackagePath = buildPackagePath(modelMetadata.getSubsystem());
		String modulePackagePath = buildPackagePath(modelMetadata.getModule());
		String viewWindowFilePath = modelMetadata.getProjectLocation() + File.separator + modelMetadata.getWebRoot() + File.separator + subsystemPackagePath + File.separator + modulePackagePath + File.separator + modelMetadata.getJavaModelNameLowerCase();
		String viewWindowFileName = modelMetadata.getViewCapPrefix() + modelMetadata.getJavaModelNameLowerCamelCase() + JS_EXT;
		
		String template = EXTJS_TEMPLATE_PATH + "/ViewWindow.ftl";
		return "@" + generateCode(modelMetadata, template, viewWindowFilePath, viewWindowFileName);
	}
	

	public String generateGridPanelCode(ModelMetadata modelMetadata) {
		String subsystemPackagePath = buildPackagePath(modelMetadata.getSubsystem());
		String modulePackagePath = buildPackagePath(modelMetadata.getModule());
		String gridPanelFilePath = modelMetadata.getProjectLocation() + File.separator + modelMetadata.getWebRoot() + File.separator + subsystemPackagePath + File.separator + modulePackagePath + File.separator + modelMetadata.getJavaModelNameLowerCase();
		String gridPanelFileName = modelMetadata.getJavaModelNameLowerCamelCase() + modelMetadata.getGridPanelSuffer() + JS_EXT;
		
		String template = EXTJS_TEMPLATE_PATH + "/GridPanel.ftl";
		return "@" + generateCode(modelMetadata, template, gridPanelFilePath, gridPanelFileName);
	}

}
